package com.youzhi.backend.exception;


import cn.hutool.core.util.StrUtil;
import lombok.Data;

/**
 * 业务异常
 *
 * @author mengxu
 */
@Data
public class YouZhiException extends RuntimeException {


    private int code;

    private String msg;


    public YouZhiException(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public YouZhiException(int code, String msg, Throwable throwable) {
        super(throwable);
        this.code = code;
        this.msg = msg;
    }


    public YouZhiException(EcpEnum ecpEnum) {
        this.code = ecpEnum.code;
        this.msg = ecpEnum.msg;
    }


    public YouZhiException(EcpEnum ecpEnum, Throwable throwable) {
        super(throwable);
        this.code = ecpEnum.code;
        this.msg = ecpEnum.msg;
    }


    public static YouZhiException newException(EcpEnum ecpEnum) {
        return new YouZhiException(ecpEnum);
    }


    public static YouZhiException newException(EcpEnum ecpEnum, Throwable throwable) {
        return new YouZhiException(ecpEnum, throwable);
    }


    public static YouZhiException newException(EcpEnum ecpEnum,String variable, Throwable throwable) {
        String msg = StrUtil.replace(ecpEnum.msg, "{}", variable);
        return new YouZhiException(ecpEnum.code, msg,throwable);
    }


    public static YouZhiException newException(int code, String msg) {
        return new YouZhiException(code, msg);
    }

    public static YouZhiException newException(int code, String msg, Throwable throwable) {
        return new YouZhiException(code, msg, throwable);
    }

    public static YouZhiException newUnknownException() {
        return new YouZhiException(EcpEnum.unknown_error);
    }


    public static YouZhiException newException(EcpEnum ecpEnum, String variable) {
        String msg = StrUtil.replace(ecpEnum.msg, "{}", variable);
        return newException(ecpEnum.code, msg);
    }


}
